package com.handpay.zztong.hp.swiper.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.swiper.model.SwiperBluetoothDeviceSelectInfo;

import java.util.ArrayList;
import java.util.List;

public class SwiperBluetoothAdapter extends BaseAdapter {

	private Context context;
	private List<SwiperBluetoothDeviceSelectInfo> deviceInfos;
	private String mNameFilter;
	private String mDeviceName;

	public SwiperBluetoothAdapter(Context c, String filter) {
		super();
		context = c;
		deviceInfos = new ArrayList<SwiperBluetoothDeviceSelectInfo>();
		mNameFilter = filter;	
	}
	public void setDeviceName(String name){
		mDeviceName = name;
	}

	public void clear() {
		deviceInfos.clear();
		this.notifyDataSetChanged();
	}

	public void addDevice(SwiperBluetoothDeviceSelectInfo deviceInfo) {
		if (deviceInfo.getName().contains(mNameFilter)) {
			deviceInfos.add(deviceInfo);
			this.notifyDataSetChanged();
		}
	}

	public SwiperBluetoothDeviceSelectInfo getDeviceInfo(int position) {
		return deviceInfos.get(position);
	}

	@Override
	public int getCount() {
		return deviceInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	private static class ViewHodler {
		TextView tvPosName;
		TextView tvMac;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler viewHodler = null;
		if (convertView == null) {
			viewHodler = new ViewHodler();
			LayoutInflater l = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = l.inflate(
					R.layout.item_swiper_bluetooth_list, null);

			viewHodler.tvPosName = (TextView) convertView
					.findViewById(R.id.landi_tv_qpos_name);

			viewHodler.tvMac = (TextView) convertView
					.findViewById(R.id.landi_tv_bond_status);
			convertView.setTag(viewHodler);
		}else {
			viewHodler = (ViewHodler) convertView.getTag();
		}

		viewHodler.tvPosName.setText(mDeviceName);

		viewHodler.tvMac.setText(deviceInfos.get(position).getName());

		return convertView;
	}

}
